<?php
namespace Common\Api;
class WechatCrypt{
	private $cyptKey = '';
	private $appId = '';

	public function __construct($key, $appid){
		if($key && $appid){
			$this->appId = $appid;
			$this->cyptKey = base64_decode($key . '=');
		}else{
			throw new \Exception('缺少参数 APP_ID 和加密KEY!');
		}
	}

	public function encrypt($text){
		$random = getRandomString(16);
		$size = pack("N", strlen($text));
		$text = $random . $size . $text . $this->appId;
		$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
		$text = self::PKCS7Encode($text, mcrypt_enc_get_key_size($td));
		mcrypt_generic_init($td, $this->cyptKey, substr($this->cyptKey, 0, 16));
		$encrypt = mcrypt_generic($td, $text);
		mcrypt_generic_deinit($td);
		mcrypt_module_close($td);
		return base64_encode($encrypt);
	}

	public function decrypt($encrypt){
		$encrypt = base64_decode($encrypt);
		$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
		mcrypt_generic_init($td, $this->cyptKey, substr($this->cyptKey, 0, 16));
		$decrypt = mdecrypt_generic($td, $encrypt);
		$decrypt = self::PKCS7Decode($decrypt, mcrypt_enc_get_key_size($td));
		mcrypt_generic_deinit($td);
		mcrypt_module_close($td);
		if(strlen($decrypt) < 16){
			throw new \Exception("非法密文字符串！");
		}
		$decrypt = substr($decrypt, 16);
		$size = unpack("N", substr($decrypt, 0, 4));
		$size = $size[1];
		$appid = substr($decrypt, $size + 4);
		if($appid !== $this->appId){
			throw new \Exception("非法APP_ID！");
		}
		return substr($decrypt, 4, $size);
	}

	private static function PKCS7Encode($text, $size){
		$str_size = strlen($text);
		$pad_size = $size - ($str_size % $size);
		$pad_size = $pad_size ?: $size;
		$pad_chr = chr($pad_size);
		return str_pad($text, $str_size + $pad_size, $pad_chr, STR_PAD_RIGHT);
	}

	private static function PKCS7Decode($text, $size){
		$pad_str = ord(substr($text, -1));
		if($pad_str < 1 || $pad_str > $size){
			return '';
		}else{
			return substr($text, 0, strlen($text) - $pad_str);
		}
	}
}
